package com.supermap.dataservice.server.iot;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.thrid.iot.util.IotWsdlServiceFactory;
import com.supermap.dubbo.service.IotDubboService;
import com.supermap.dubbo.service.domain.ResponseData;

import java.rmi.RemoteException;

/**
 * @author xiaolu
 * @date 2018/6/7 下午5:39
 */
@Service(version = "1.0.0")
public class IotDubboServiceImpl implements IotDubboService {

    @Override
    public JSONObject isValid(String userName, String password, boolean isUseMD5) {
        boolean valid = false;
        int statusCode = 200;
        try {
            valid = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).isValid();
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, valid).toFastJson();
    }

    @Override
    public JSONObject getRegions(String userName, String password, boolean isUseMD5) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getRegions();
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getWeatherPointsByRegionId(String userName, String password, boolean isUseMD5, int regionId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getWeatherPointsByRegionId(regionId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getWeatherPointsByBMId(String userName, String password, boolean isUseMD5, int bmId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getWeatherPointsByBMId(bmId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getNodesByRegionID(String userName, String password, boolean isUseMD5, int regionId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getNodesByRegionID(regionId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getNodesByBMID(String userName, String password, boolean isUseMD5, int bmId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getNodesByBMID(bmId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getMonitorCategories(String userName, String password, boolean isUseMD5, int nodeId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getMonitorCategories(nodeId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getEnvInfosByStat(String userName, String password, boolean isUseMD5, int nodeId, String time, int type) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getEnvInfosByStat(nodeId, time, type);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getEnvInfoDetails(String userName, String password, boolean isUseMD5, int nodeId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getEnvInfoDetails(nodeId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getDevices(String userName, String password, boolean isUseMD5, int nodeId) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getDevices(nodeId);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject sendDeviceCommand(String userName, String password, boolean isUseMD5, int deviceId, int openType) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).sendDeviceCommand(deviceId, openType);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject sendDeviceCommandDetail(String userName, String password, boolean isUseMD5, int deviceId, int openType, int ctrTime, int ctrIntensity) {
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).sendDeviceCommandDetail(deviceId, openType, ctrTime, ctrIntensity);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }

    @Override
    public JSONObject getMapInfo(String userName, String password, boolean isUseMD5, int regionId, int type, String code) {
        if (type == 0) {
            code = "";
        }
        Object data = null;
        int statusCode = 200;
        try {
            data = IotWsdlServiceFactory.getServiceSoap12(userName, password, isUseMD5).getMapInfo(regionId, type, code);
        } catch (RemoteException e) {
            statusCode = 404;
        }
        return new ResponseData(statusCode, data).toFastJson();
    }
}
